

#include "HeaderFiles.h"


/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/

void adc0_init(void)
{
	rcu_periph_clock_enable(RCU_GPIOA); 
	rcu_periph_clock_enable(RCU_GPIOC);   
	
	gpio_init(GPIOC, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_0);
	gpio_init(GPIOC, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_1);
	gpio_init(GPIOC, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_2);	
	gpio_init(GPIOC, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_3);
	gpio_init(GPIOC, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_4);
	gpio_init(GPIOC, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_5);	
	
	gpio_init(GPIOA, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_2);
	gpio_init(GPIOA, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_3);	
	
	/* enable ADC0 clock */
    rcu_periph_clock_enable(RCU_ADC0);
    /* config ADC clock */
    rcu_adc_clock_config(RCU_CKADC_CKAPB2_DIV2);

    /* ADC mode config */
    adc_mode_config(ADC_MODE_FREE); 
    /* ADC special function config */
    adc_special_function_config(ADC0, ADC_SCAN_MODE, ENABLE);
    adc_special_function_config(ADC0, ADC_CONTINUOUS_MODE, DISABLE);  
    /* ADC data alignment config */
    adc_data_alignment_config(ADC0, ADC_DATAALIGN_RIGHT);

    /* ADC channel length config */
    adc_channel_length_config(ADC0, ADC_REGULAR_CHANNEL, 8);
	
    /* ADC regular channel config */
    adc_regular_channel_config(ADC0, 0, ADC_CHANNEL_10, ADC_SAMPLETIME_7POINT5);
	adc_regular_channel_config(ADC0, 1, ADC_CHANNEL_11, ADC_SAMPLETIME_7POINT5);
    adc_regular_channel_config(ADC0, 2, ADC_CHANNEL_12, ADC_SAMPLETIME_7POINT5);
    adc_regular_channel_config(ADC0, 3, ADC_CHANNEL_13, ADC_SAMPLETIME_7POINT5);
    adc_regular_channel_config(ADC0, 4, ADC_CHANNEL_14, ADC_SAMPLETIME_7POINT5);
    adc_regular_channel_config(ADC0, 5, ADC_CHANNEL_15, ADC_SAMPLETIME_7POINT5);
    adc_regular_channel_config(ADC0, 6, ADC_CHANNEL_4 , ADC_SAMPLETIME_7POINT5);
    adc_regular_channel_config(ADC0, 7, ADC_CHANNEL_5 , ADC_SAMPLETIME_7POINT5);
	
    /* ADC trigger config */
    adc_external_trigger_source_config(ADC0, ADC_REGULAR_CHANNEL, ADC0_1_2_EXTTRIG_REGULAR_NONE); 
	
    /* ADC external trigger enable */
    adc_external_trigger_config(ADC0, ADC_REGULAR_CHANNEL, ENABLE);
    
    /* clear the ADC flag */
    adc_interrupt_flag_clear(ADC0, ADC_INT_FLAG_EOC);
    adc_interrupt_flag_clear(ADC0, ADC_INT_FLAG_EOIC);
    /* enable ADC interrupt */
    adc_interrupt_enable(ADC0, ADC_INT_EOC);

    /* enable ADC interface */
    adc_enable(ADC0);
    delay_1ms(1);
    /* ADC calibration and reset calibration */
    adc_calibration_enable(ADC0);

    /* ADC DMA function enable */
    adc_dma_mode_enable(ADC0);

}

/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/

void adc2_init(void)
{

	rcu_periph_clock_enable(RCU_GPIOF);   
	gpio_init(GPIOF, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_6);
	gpio_init(GPIOF, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_7);
	gpio_init(GPIOF, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_8);	
	gpio_init(GPIOF, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_9);
	gpio_init(GPIOF, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_10);
	
	/* enable ADC2 clock */
    rcu_periph_clock_enable(RCU_ADC2);
    /* config ADC clock */
    rcu_adc_clock_config(RCU_CKADC_CKAPB2_DIV2);

    /* ADC mode config */
    adc_mode_config(ADC_MODE_FREE); 
    /* ADC special function config */
    adc_special_function_config(ADC2, ADC_SCAN_MODE, ENABLE);
    adc_special_function_config(ADC2, ADC_CONTINUOUS_MODE, DISABLE);  
    /* ADC data alignment config */
    adc_data_alignment_config(ADC2, ADC_DATAALIGN_RIGHT);

    /* ADC channel length config */
    adc_channel_length_config(ADC2, ADC_REGULAR_CHANNEL, 5);
	
    /* ADC regular channel config */	
	adc_regular_channel_config(ADC2, 0, ADC_CHANNEL_4, ADC_SAMPLETIME_7POINT5);
	adc_regular_channel_config(ADC2, 1, ADC_CHANNEL_5, ADC_SAMPLETIME_7POINT5);
	adc_regular_channel_config(ADC2, 2, ADC_CHANNEL_6, ADC_SAMPLETIME_7POINT5);
	adc_regular_channel_config(ADC2, 3, ADC_CHANNEL_7, ADC_SAMPLETIME_7POINT5);
	adc_regular_channel_config(ADC2, 4, ADC_CHANNEL_8, ADC_SAMPLETIME_7POINT5);
	
    /* ADC trigger config */
    adc_external_trigger_source_config(ADC2, ADC_REGULAR_CHANNEL, ADC0_1_2_EXTTRIG_REGULAR_NONE); 
	
    /* ADC external trigger enable */
    adc_external_trigger_config(ADC2, ADC_REGULAR_CHANNEL, ENABLE);
    
    /* clear the ADC flag */
    adc_interrupt_flag_clear(ADC2, ADC_INT_FLAG_EOC);
    adc_interrupt_flag_clear(ADC2, ADC_INT_FLAG_EOIC);

    /* enable ADC interface */
    adc_enable(ADC2);
    delay_1ms(1);
    /* ADC calibration and reset calibration */
    adc_calibration_enable(ADC2);

    /* ADC DMA function enable */
    adc_dma_mode_enable(ADC2);


}


/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/

void adc_init(void)
{
	adc0_init();
	adc2_init();	
	
}


